package cn.neusoft.neusoft_fz.utils;

import cn.neusoft.neusoft_fz.entity.DeliveryEntity;
import cn.neusoft.neusoft_fz.entity.InvoiceEntity;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.TextAlignment;
import org.example.entity.Invoice;

import java.util.Objects;


public class PDFUtils {


    public void createInvoicePdf(String dest, InvoiceEntity invoiceEntity) throws Exception {

        // 创建字体
        PdfFont font = PdfFontFactory.createFont("fonts/SIMHEI.TTF", PdfEncodings.IDENTITY_H);

        PdfWriter writer = new PdfWriter(dest);
        PdfDocument pdf = new PdfDocument(writer);
        Document document = new Document(pdf);

        document.setFont(font);

        // 设置标题
        Paragraph title = new Paragraph("发票")
                .setFont(font)
                .setFontSize(20)
                .setTextAlignment(TextAlignment.CENTER); // 设置标题居中
        title.setFontColor(ColorConstants.RED);  // 设置标题颜色为红色
        document.add(title);

        // 创建表格
        Table table = new Table(2);

        // 添加数据到表格
        addRowToTable(table, "发票编号", String.valueOf(invoiceEntity.getInvoiceId()), font);
        addRowToTable(table, "开票金额", String.valueOf(invoiceEntity.getInvoiceAmount()), font);
        addRowToTable(table, "所属分站", invoiceEntity.getSubstationName(), font);
        addRowToTable(table, "客户姓名", "客户", font);

        table.setHorizontalAlignment(HorizontalAlignment.CENTER);

        document.add(table);
        document.close();
    }

    public void createDeliveryPdf(String dest, DeliveryEntity deliveryOrder) throws Exception {
        // 创建字体
        PdfFont font = PdfFontFactory.createFont("fonts/SIMHEI.TTF", PdfEncodings.IDENTITY_H);
        PdfWriter writer = new PdfWriter(dest);
        PdfDocument pdf = new PdfDocument(writer);
        Document document = new Document(pdf);
        document.setFont(font);
        // 设置标题
        Paragraph title = new Paragraph("配送单")
                .setFont(font)
                .setFontSize(20)
                .setTextAlignment(TextAlignment.CENTER); // 设置标题居中
        title.setFontColor(ColorConstants.RED);  // 设置标题颜色为红色
        document.add(title);
        // 创建表格
        Table table = new Table(2);
        // 添加数据到表格
        addRowToTable(table, "配送单号", String.valueOf(deliveryOrder.getDeliveryOrderId()), font);
        addRowToTable(table, "收件人", deliveryOrder.getConsumerName(), font);
        addRowToTable(table, "收件人地址", deliveryOrder.getConsumerAddress(), font);
        addRowToTable(table, "配送员姓名", deliveryOrder.getName(), font);
        addRowToTable(table, "联系信息", deliveryOrder.getContactInfo(), font);
        addRowToTable(table, "收件人电话", deliveryOrder.getConsumerPhone(), font);
        addRowToTable(table, "配送状态", deliveryOrder.getDeliveryStatus(), font);
        addRowToTable(table, "配送分站", deliveryOrder.getSubstationName(), font);
        table.setHorizontalAlignment(HorizontalAlignment.CENTER);
        document.add(table);
        document.close();
    }

    // 自定义方法，用于向表格添加行
    private static void addRowToTable(Table table, String header, String value, PdfFont font) {
        Paragraph headerParagraph = new Paragraph(header).setFont(font).setFontColor(ColorConstants.BLUE); // 设置头部颜色为蓝色
        Paragraph valueParagraph = new Paragraph(value).setFont(font);
        if (Objects.equals(value, "已完成")){
            valueParagraph.setFontColor(ColorConstants.GREEN);
        }else if (Objects.equals(value, "进行中")){
            valueParagraph.setFontColor(ColorConstants.GRAY);
        }else if (Objects.equals(value, "未开始")){
            valueParagraph.setFontColor(ColorConstants.PINK);
        }
        table.addCell(headerParagraph);
        table.addCell(valueParagraph);
    }
}
